﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using MYNAMESPACE;
using System.Net.Mail;

namespace final_project
{
    public partial class Bay_Bills : Form
    {
        private connect_db con;
        public SmtpClient client = new SmtpClient();
        public MailMessage msg = new MailMessage();
        public System.Net.NetworkCredential smpt =
            new System.Net.NetworkCredential("hima4105@gmail.com", "0562192218");
        public string nm2;
        public string passvalue
        {

            get
            {
                return nm2;
            }
            set
            {
                nm2 = value;
            }

        }
       
        public Bay_Bills()
        {
            InitializeComponent();
        }
        public void SendEmail(string sendTo, string sendFrom, string subject, string body)
        {
            try
            {
                client.Host = "smtp.gmail.com";
                client.Port = 587;
                client.UseDefaultCredentials = false;
                client.Credentials = smpt;
                client.EnableSsl = true;

                MailAddress to = new MailAddress(sendTo);
                MailAddress from = new MailAddress(sendFrom);

                msg.Subject = subject;
                msg.Body = body;
                msg.From = from;
                msg.To.Add(to);

                client.Send(msg);



            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error");

            }
        }
        private void Bay_Bills_Load(object sender, EventArgs e)
        {
            ID.Text = nm2;
            user US = new user();

            US.setnational_id(Int32.Parse(ID.Text.Trim()));


            con = new connect_db();
            con.sqlquery("select balance from userdata where id=(@namep)");
            con.cmd.Parameters.Add("@namep", US.getnational_id());
            foreach (DataRow dr in con.QueryEx().Rows)
            {

                Balance.Text = dr[0].ToString();


            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Validating Start code

            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                MessageBox.Show("All controls are valid!");
                //Logic to save...
            }
            else
            {
                MessageBox.Show("There are invalid controls on the form.");
                //Return user to form...
            }

            //Validating end code 



            if (How_Much.Text.Length == 0 || TO_ID.Text.Length == 0)
            {

                MessageBox.Show("must have value");

            }
            else
            {
                user US = new user();
                user US_to = new user();
                US.setnational_id(Int32.Parse(ID.Text.Trim()));
                US_to.setnational_id(Int32.Parse(TO_ID.Text.Trim()));
                US.setbalance(double.Parse(Balance.Text));


                con = new connect_db();
                con.sqlquery("select id from userdata where id=(@namep)");
                con.cmd.Parameters.Add("@namep", US_to.getnational_id());
                int count = 0;
                foreach (DataRow dr in con.QueryEx().Rows)
                {

                    count = count + 1;


                }
                if (count == 1 && US.getnational_id() != US_to.getnational_id())
                {

                    if (US.withdrwal(double.Parse(How_Much.Text)))
                    {
                        con = new connect_db();
                        con.sqlquery("update userdata set balance = (@namep1) where id= (@namep) ");
                        con.cmd.Parameters.Add("@namep", US.getnational_id());
                        con.cmd.Parameters.Add("@namep1", US.getbalance());
                        con.sqlnonex();
                        con.sqlquery("select balance from userdata where id=(@namep)");
                        con.cmd.Parameters.Add("@namep", US.getnational_id());
                        foreach (DataRow dr in con.QueryEx().Rows)
                        {

                            MessageBox.Show("the balance is " + dr[0].ToString());


                        }
                        con = new connect_db();
                        con.sqlquery("select balance from userdata where id=(@namep)");
                        con.cmd.Parameters.Add("@namep", US_to.getnational_id());
                        foreach (DataRow dr in con.QueryEx().Rows)
                        {

                            TO_Balance.Text = dr[0].ToString();


                        }
                        US_to.setbalance(double.Parse(TO_Balance.Text));
                        US_to.deposite(double.Parse(How_Much.Text.Trim()));
                        con = new connect_db();
                        con.sqlquery("update userdata set balance = (@namep1) where id= (@namep) ");
                        con.cmd.Parameters.Add("@namep", US_to.getnational_id());
                        con.cmd.Parameters.Add("@namep1", US_to.getbalance());
                        con.sqlnonex();
                        con.sqlquery("select balance from userdata where id=(@namep)");
                        con.cmd.Parameters.Add("@namep", US_to.getnational_id());
                        foreach (DataRow dr in con.QueryEx().Rows)
                        {

                            TO_Balance.Text = dr[0].ToString();


                            con.sqlquery("select mail from userdata where id=(@namep)");
                            con.cmd.Parameters.Add("@namep", US_to.getnational_id());
                            foreach (DataRow dr1 in con.QueryEx().Rows)
                            {
                                SendEmail(dr1[0].ToString() + "@gmail.com", "hima4105@gmail.com", "Bank System", "YOUR VALUE BECOME   " + dr[0].ToString() + "    from  id is   " + ID.Text);
                            }
                        }
                        this.Hide();
                        Account acc = new Account();
                        acc.passvalue = ID.Text;
                        acc.ShowDialog();

                    }
                    else
                    {

                        MessageBox.Show("Do not have this value");
                    }

                }
                else
                {


                    MessageBox.Show("Error");

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            user US = new user();

            US.setnational_id(Int32.Parse(ID.Text.Trim()));
            this.Hide();
            Account acc = new Account();
            acc.passvalue = ID.Text;
            acc.ShowDialog();
        }

        private void How_Much_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(How_Much.Text))
            {

                e.Cancel = true;
                How_Much.Focus();
                errorProvider1.SetError(How_Much, "Salary Must Be Entered !");

            }
            else if (!Regex.IsMatch(How_Much.Text, @"^\d+$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(How_Much, "No Negative Numbers");

            }
            else
            {
                e.Cancel = false;
                errorProvider1.SetError(How_Much, "");
            }
        }

        private void TO_ID_Validating(object sender, CancelEventArgs e)
        {


            if (string.IsNullOrEmpty(TO_ID.Text))
            {

                e.Cancel = true;
                TO_ID.Focus();
                errorProvider1.SetError(TO_ID, "ID Must Be Entered !");

            }

            else if (!Regex.IsMatch(TO_ID.Text, @"^\d{4}$"))
            {
                e.Cancel = true;
                errorProvider1.SetError(TO_ID, "Valid ID 4 Number");

            }



            else if (Regex.IsMatch(TO_ID.Text, @"^\d{4}$"))
            {
                e.Cancel = false;
                errorProvider1.SetError(TO_ID, "");

            }
            else { }


        }
    }
}
